
#include <btPointConstraint.h>

#include <BLabel>
#include <BGridLayout>
#include <BVBoxLayout>
#include <BHBoxLayout>
#include <BGroupBox>

#include "StringUtils.h"
#include "PropertyBoard.h"

PropertyBoard::PropertyBoard(BWidget* parent) : BWidget(parent)
{
	this->setTitle("Push Parameters");

	_nodeGroupBox = new BGroupBox("Node");
	{
		_positionEdit = new BLineEdit();
		_rotateEdit = new BLineEdit();
		BGridLayout* gridLayout = new BGridLayout(_nodeGroupBox);
		gridLayout->setWidget(0, 0, new BLabel("position"));
		gridLayout->setWidget(0, 1, _positionEdit);
		gridLayout->setWidget(1, 0, new BLabel("rotate"));
		gridLayout->setWidget(1, 1, _rotateEdit);
	}

	_physicsGroupBox = new BGroupBox("Physics");
	_physicsGroupBox->setCheckable(true);
	_physicsGroupBox->setChecked(true);
	{
		BHBoxLayout* massLayout = new BHBoxLayout();
		{
			BLabel* massLabel = new BLabel("Mass");
			_massSpinBox = new BRealSpinBox();
			_massSpinBox->setSuffix("KG");
			massLayout->addWidget(massLabel);
			massLayout->addWidget(_massSpinBox);
		}

		BLabel* forceLabel = new BLabel("Force");
		BGridLayout* forceLayout = new BGridLayout();
		{
			_forceXSpinBox = new BRealSpinBox();
			_forceYSpinBox = new BRealSpinBox();
			_forceZSpinBox = new BRealSpinBox();
			forceLayout->setWidget(1, 0, new BLabel("X"));
			forceLayout->setWidget(1, 1, _forceXSpinBox);
			forceLayout->setWidget(2, 0, new BLabel("Y"));
			forceLayout->setWidget(2, 1, _forceYSpinBox);
			forceLayout->setWidget(3, 0, new BLabel("Z"));
			forceLayout->setWidget(3, 1, _forceZSpinBox);
		}

		BVBoxLayout* vlayout = new BVBoxLayout(_physicsGroupBox);
		vlayout->addLayout(massLayout);
		vlayout->addWidget(forceLabel);
		vlayout->addLayout(forceLayout);
	}

	BVBoxLayout* vlayout = new BVBoxLayout(this);
	vlayout->addWidget(_nodeGroupBox);
	vlayout->addWidget(_physicsGroupBox);

	connect(this, Signal_AssetChanged, &PropertyBoard::slotAssetChanged);

	connect(_positionEdit, Signal_EditFinished, &PropertyBoard::slotPositionEditFinished);
	connect(_rotateEdit, Signal_EditFinished, &PropertyBoard::slotRotateEditFinished);
	connect(_massSpinBox, Signal_EditFinished, &PropertyBoard::slotMassEditFinished);
	connect(_forceXSpinBox, Signal_EditFinished, &PropertyBoard::slotForceEditFinished);
	connect(_forceYSpinBox, Signal_EditFinished, &PropertyBoard::slotForceEditFinished);
	connect(_forceZSpinBox, Signal_EditFinished, &PropertyBoard::slotForceEditFinished);
}
PropertyBoard::~PropertyBoard()
{

}

void PropertyBoard::slotAssetChanged(BObject* object, const BValue& value)
{
	_node = value;
	if (_node)
	{
		if (_node)
		{
			const BVector& position = _node->position();
			_positionEdit->setText(StringFromVector(position));
			_rotateEdit->setText(StringFromQuater(_node->rotate()));
		}
		if (BPhysics* physics = _node->physics())
		{
			BReal mass = physics->mass();
			_massSpinBox->setValue(mass);
			const BVector& force = physics->force();
			_forceXSpinBox->setValue(force.x());
			_forceYSpinBox->setValue(force.y());
			_forceZSpinBox->setValue(force.z());
		}
	}
}

void PropertyBoard::slotPositionEditFinished(BObject* object, const BValue& value)
{

}
void PropertyBoard::slotRotateEditFinished(BObject* object, const BValue& value)
{

}

void PropertyBoard::slotMassEditFinished(BObject* object, const BValue& value)
{

}
void PropertyBoard::slotForceEditFinished(BObject* object, const BValue& value)
{

}
